package string;

public class StringBuilderDemo {
    public static void main(String[] args) {
        String str = "好好学习Java";
        //复制str的内容到builder中---好好学习Java
        StringBuilder builder = new StringBuilder(str);

        //append():追加内容--在末尾追加
        builder.append(",为了找个好工作");
        System.out.println(builder);

        //replace():替换部分内容(含头不含尾)
        builder.replace(9,16,"就是为了改变世界");
        System.out.println(builder);
        builder.replace(9,18,"就是为了改变世界");//即使大于最大长度也不会报错,后面接空字符
        System.out.println(builder);

        //delete():删除部分内容(含头不含尾)
        builder.delete(1,8);//删除下标为1到7
        System.out.println(builder);//好,就是为了改变世界

        //insert():插入内容
        builder.insert(1,"活着");
        System.out.println(builder);//活着,就是为了改变世界


        /*
        //StringBuilder的创建方式:
        StringBuilder builder1 = new StringBuilder();//空字符串
        StringBuilder builder2 = new StringBuilder("abc");//abc串

        //String和StringBuilder互转:
        String str = "abc";
        StringBuilder builder = new StringBuilder(str);//abc串
        String str2 = builder.toString();
         */
    }
}
